package cn.com.haopy.yue.ai.db.service.hos.impl;


import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskAd;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskAdMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskAdService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2023/9/25 14:49
 */
@Service
@Slf4j
public class AIRiskAdServiceImpl extends BaseServiceImpl<AIRiskAdMapper, AIRiskAd> implements AIRiskAdService {
    @Override
    public Page<AIRiskAd> page(String name, Integer page, Integer limit, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }

        QueryWrapper<AIRiskAd> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(AIRiskAd::getName, name);
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return page(new Page<>(page, limit), queryWrapper);
    }

    public List<AIRiskAd> listByPosition(Short position) {
        QueryWrapper<AIRiskAd> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(position)) {
            queryWrapper.lambda().eq(AIRiskAd::getPosition, position);
        }
        queryWrapper.lambda().orderByDesc(AIRiskAd::getSortOrder);
        return list(queryWrapper);
    }

    @Override
    public List<AIRiskAd> listByIds(List<Integer> ids) {
        QueryWrapper<AIRiskAd> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(ids) && ids.size() > 0) {
            queryWrapper.lambda().in(AIRiskAd::getId, ids);
        }
        queryWrapper.lambda().orderByDesc(AIRiskAd::getSortOrder);
        return list(queryWrapper);
    }
}
